package route;

import model.*;
import service.ServiceAuth;

import java.util.HashMap;

public class RouteAuth {
    public static void log(String format, Object... args) {
        System.out.println(String.format(format, args));
    }

    /**
     * 处理GET登录请求,登录视图
     *
     * @param request
     * @return
     */
    public static String loginView(Request request) {
        // try {
        //     Thread.sleep(1000000);
        // } catch (InterruptedException e) {
        //     throw new RuntimeException(e);
        // }
        User user = Route.currentUser(request);
        String username = user.username;
        String message = String.format("当前登录用户 %s", username);

        // 读取模板文件
        HashMap<String, String> data = new HashMap<>();
        data.put("message", message);
        String body = NewTemplate.render(data, "login.ftlh");
        return Route.responseHTML(body);
    }

    /**
     * 处理POST登录请求
     *
     * @param request
     * @return
     */
    public static String login(Request request) {
        log("%s %s", request, request.cookie);
        String message;
        String cookie;
        String username = request.form.get("username");
        String password = request.form.get("password");
        String sessionId = ServiceAuth.login(username, password);
        if (sessionId.length() > 0) {
            cookie = String.format("Set-Cookie: session_id=%s\r\n", sessionId);
            message = String.format("登录成功 %s", username);
        } else {
            cookie = "";
            message = "登录失败";
        }
        HashMap<String, String> data = new HashMap<>();
        data.put("message", message);
        String body = NewTemplate.render(data, "login.ftlh");
        return Route.responseHTML(body, cookie);
    }

    /**
     * 处理GET注册请求，注册视图
     *
     * @return
     */
    public static String registerView() {
        // 读取模板文件
        HashMap<String, String> data = new HashMap<>();
        data.put("message", "请注册");
        String body = NewTemplate.render(data, "register.ftlh");
        return Route.responseHTML(body);
    }

    /**
     * 处理POST注册请求
     * @param request
     * @return
     */
    public static String register(Request request) {
        String message;
        String username = request.form.get("username");
        String password = request.form.get("password");
        boolean success = ServiceAuth.register(username, password);
        if (success) {
            message = String.format("注册成功 %s", username);
        } else {
            message = String.format("注册失败");
        }

        HashMap<String, String> data = new HashMap<>();
        data.put("message", message);
        String body = NewTemplate.render(data, "register.ftlh");
        return Route.responseHTML(body);
    }

    // 进入修改密码页面
    public static String updateView() {
        HashMap<String, String> data = new HashMap<>();
        data.put("message", "请修改密码");
        String body = NewTemplate.render(data, "updatePassword.ftlh");
        return Route.responseHTML(body);
    }

    // 修改密码
    public static String updatePassword(Request request) {
        // 拿到修改用户名和修改的密码
        String username = request.form.get("username");
        String newPassword = request.form.get("password");
        // 处理数据
        String result = ServiceAuth.updatePassword(username, newPassword);
        //返回响应
        return result;
    }

    // 退出登录
    public static String exit(Request request) {
        User currentUser = Route.currentUser(request);
        ServiceAuth.exit(currentUser.username);
        HashMap<String, String> data = new HashMap<>();
        data.put("message", "您已退出登录！");
        String body = NewTemplate.render(data, "login.ftlh");
        return Route.responseHTML(body);
    }
}
